package ldh.im.monitor.dao;

import ldh.im.fxbase.util.DbUtil;
import ldh.im.fxbase.util.StringUtil;
import ldh.im.monitor.pojo.ImMonitorUser;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class ImMonitorUserDao {

    private static final String TABLE_NAME = "im_monitor_user";

    public ImMonitorUser getLoginByUserId(String userId) throws SQLException {
        return DbUtil.query((queryRunner, connection) -> {
            String sql = String.format("select * from im_monitor_user where mobile = ?");
            ImMonitorUser imLogin = queryRunner.query(connection, sql, new BeanHandler<ImMonitorUser>(ImMonitorUser.class), userId);
            return imLogin;
        });
    }

    public ImMonitorUser getLoginForLasted() throws SQLException {
        return DbUtil.query((queryRunner, connection) -> {
            String sql = String.format("select * from im_monitor_user where token = '' order by updateTime desc limit 0, 1");
            ImMonitorUser imLogin = queryRunner.query(connection, sql, new BeanHandler<ImMonitorUser>(ImMonitorUser.class));
            return imLogin;
        });
    }

    public void insertLogin(ImMonitorUser imMonitorUser) throws SQLException {
        DbUtil.transaction((queryRunner, connection) -> {
            ResultSetHandler<Long> resultSetHandler = new ScalarHandler();
            String sql = String.format("insert into im_monitor_user(mobile, token, maskPassword, updateTime) values(?, ?, ?, ?)");
            Long id = queryRunner.insert(connection, sql, resultSetHandler, imMonitorUser.getMobile(), imMonitorUser.getToken(), imMonitorUser.getMaskPassword(), imMonitorUser.getUpdateTime());
            return imMonitorUser;
        });
    }

    public List<ImMonitorUser> getLogins() throws SQLException {
        return DbUtil.transaction((queryRunner, connection) -> {
            String sql = String.format("select * from im_monitor_user");
            return queryRunner.query(connection, sql, new BeanListHandler<ImMonitorUser>(ImMonitorUser.class));
        });
    }

    public void updateLogin(ImMonitorUser imMonitorUser) throws SQLException {
        DbUtil.transaction((queryRunner, connection) -> {
            if (StringUtil.isEmpty(imMonitorUser.getToken())) {
                String sql = String.format("update im_monitor_user set maskPassword = ?, updateTime  = ? where mobile = ?");
                queryRunner.update(connection, sql, imMonitorUser.getMaskPassword(), imMonitorUser.getUpdateTime(), imMonitorUser.getMobile());
            } else {
                String sql = String.format("update im_monitor_user set token = ?, maskPassword = ?, updateTime  = ? where mobile = ?");
                queryRunner.update(connection, sql, imMonitorUser.getToken(), imMonitorUser.getMaskPassword(), imMonitorUser.getUpdateTime(), imMonitorUser.getMobile());
            }
            return imMonitorUser;
        });
    }
}
